<?php

namespace App\Http\Controllers\Home;

use App\Models\Open;
use App\Models\WechatSet;

class ServerController extends BaseController
{

    public function __construct(Open $open, WechatSet $wechatSet)
    {
        $this->open = $open;
        $this->wechatSet = $wechatSet;
    }

    //公众号授权接收URL
    public function anyAuth()
    {
        $nodeNames = ['ComponentVerifyTicket','AuthorizerAppid', 'InfoType', 'AuthorizationCode', 'PreAuthCode'];
        $msgData = $this->open->getMsgData($nodeNames);
        //获取component_verify_ticket
        $this->open->getComponentVerifyTicketFromXml($msgData);
        tolog('auth.msgData');
        tolog($msgData);
        $appid = $msgData['AuthorizerAppid'];

        $wechatSet = new WechatSet();
        $where['wx_app_id'] = $appid;
        $where['or'] = ['or', ['xcx_app_id' => $appid]];
        $wechat = $wechatSet->_getOne($where);
        $type = $wechat['wx_app_id'] == $appid ? 'wx' : 'xcx';

        $msgType = $msgData['InfoType'];

        switch ($msgType) {
            //授权成功通知
            case 'authorized' :
                $auth_code = $msgData['InfoType'];
                tolog('auth_code:' . $auth_code);
                // $wechatSet->_update(['id' => $wechat['id']], [$type . '_authorization_code' => $auth_code]);
                // $wechatSet->_redisGenerate($wechat['admin_id']);
                // $this->open->getAuthorizationAcessToken($wechat['admin_id'], $type);
                break;
            //取消授权通知
            case 'unauthorized' :
                $wechatSet->_update(['id' => $wechat['id']], [$type . '_authorization_code' => '']);
                break;
            //授权更新通知
            case 'updateauthorized' :
//                $auth_code = $msgData['AuthorizationCode'];
//                $pre_auth_code = $msgData['PreAuthCode'];
//                $wechatSet->_update(['id' => $wechat['id']], [$type . '_authorization_code' => $auth_code]);
//                $wechatSet->_redisGenerate($wechat['admin_id']);
//                $this->open->getAuthorizationAcessToken($wechat['admin_id'], $type);
                break;
        }

    }


    //公众号消息与事件接收URL
    public function anyRecive()
    {
        $nodeNames = ['Content', 'ToUserName', 'FromUserName', 'MsgType', 'Event'];
        $msgData = $this->open->getMsgData($nodeNames);
        tolog('recive.msgData');
        tolog($msgData);
        switch ($msgData['MsgType']) {
            case 'subscribe':  //关注
                return '您好，欢迎关注趣订水微信商户平台';
                break;
            case 'text':
                return $this->dealMsg($msgData);
                break;
        }
    }

    public function dealMsg($msgData)
    {
        //加密消息
        $encryptMsg = '';
        $createTime = time();
        $sendMsgContent = '这是趣订水应用平台给您回复的消息';
        $nonce = 'qds';
        $text = "<xml>
                    <ToUserName><![CDATA[{$msgData['FromUserName']}]]></ToUserName>
                    <FromUserName><![CDATA[{$msgData['ToUserName']}]]></FromUserName>
                    <CreateTime>$createTime</CreateTime>
                    <MsgType><![CDATA[text]]></MsgType>
                    <Content><![CDATA[$sendMsgContent]]></Content>
                    </xml>";
        $errCode = $this->open->encryptMsg($text, $createTime, $nonce, $encryptMsg);
        if ($errCode == 0) {
            tolog('加密后消息：' . $encryptMsg);
            exit($encryptMsg);
//            $url = url('server/recive') . '?timestamp=' . $createTime . '&nonce=' . $nonce;
//            $this->open->curl_post_ssl($url, $encryptMsg);
        } else {
            tolog('发送消息加密失败：' . $errCode);
        }
    }

}